package net.sxnic.item.print.word;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import net.sxnic.comm.basecode.BaseCodeUtils;
import net.sxnic.comm.utils.WordUtils;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.mdata.Mdata;
import net.sxnic.jhitem.itemannex.ItemAnnex;
import net.sxnic.jhitem.itemclob.ItemClob;
import net.sxnic.jhitem.itemfund.ItemFund;
import net.sxnic.jhitem.itempeople.ItemPeople;
import net.sxnic.jhitem.itemprincipal.ItemPrincipal;
import net.sxnic.jhitem.itemssmb.ItemSsmb;

import org.apache.commons.lang3.StringUtils;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImportFormatMode;

/**
 * 项目基本信息
 * 
 * @author CGD
 * 
 */
public class CyhInfo {
	static {
		Document.setLicence("sj_laokai");
	}
	
	private static int SSMBROWINDEX = 2;
	
	private static int PEOPLEROWINDEX = 31;
	
	private static int TABLEINDEXSSMB = 2;
	
	private static int TABLEINDEXPEOPLE = 3;
	
	private static int SSMBROWMAX = 8;
	
	private static int PEOPLEROWMAX = 8;

	public static void createWord(String saveDocFile, String templateFile,
			Mdata mdata, ItemFund fund, ItemPrincipal principal, ItemClob clob,
			List<ItemPeople> peoples, List<ItemSsmb> ssmbs, List<ItemAnnex> annex002List,String basePath) {
		Document doc;
		try {
			File file = new File(saveDocFile);
			if (!file.getParentFile().exists()
					&& !file.getParentFile().mkdirs()) {
				return;
			}
			
			
			doc = new Document(templateFile);
			DocumentBuilder builder = new DocumentBuilder(doc);
			SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			
			// 设置标签
			WordUtils.setBookmarks(doc, mdata);
			WordUtils.setBookmarks(doc, clob);
			WordUtils.setBookmarks(doc, principal);
			WordUtils.setBookmarks(doc, fund);
			WordUtils.setBookmarks(doc, ssmbs);

			WordUtils.setBookmark(doc, "techField",
					BaseCodeUtils.getInfoName("017", mdata.getTechField()));
			WordUtils.setBookmark(doc, "industry",
					BaseCodeUtils.getInfoName("053", mdata.getIndustry()));
			WordUtils.setBookmark(doc, "techFrom",
					BaseCodeUtils.getInfoName("016", mdata.getTechFrom()));
			WordUtils.setBookmark(doc, "unitType",
					BaseCodeUtils.getInfoName("028", mdata.getUnitType()));
			if(StringUtils.isNotBlank(mdata.getZzdwCode()))
				WordUtils.setBookmark(doc, "zzdwCode",
						ItemConstants.ZZDW_MAP.get(ItemConstants.SYSTEM_CALALOG_ALIAS).get(mdata.getZzdwCode()));
			WordUtils.setBookmark(doc, "sex",
					BaseCodeUtils.getInfoName("002", principal.getSex()));
			WordUtils.setBookmark(doc, "techTitle",
					BaseCodeUtils.getInfoName("014", principal.getTechTitle()));
			WordUtils.setBookmark(doc, "highestOffer",
					BaseCodeUtils.getInfoName("007", principal.getHighestOffer()));
			WordUtils.setBookmark(doc, "techStandard",
					BaseCodeUtils.getInfoName("015", mdata.getTechStandard()));
			WordUtils.setBookmark(doc, "judgeMode",
					BaseCodeUtils.getInfoName("023", mdata.getJudgeMode()));
			WordUtils.setBookmark(doc,"soperCode",
					ItemConstants.OPERATION_MAP.get(mdata.getSoperCode()));
			//项目分阶段具体实施目标
			if (ssmbs != null && ssmbs.size() > 0) {
				builder.moveToBookmark("ssmb_table");

				int j = SSMBROWINDEX;// 开始表格Row, 这里的index从0开始
				int tableIndex = TABLEINDEXSSMB;

				for (ItemSsmb ssmb : ssmbs) {
					// 实施时间区间
					builder.moveToCell(tableIndex, j, 0, 0);
					builder.write(ssmb.getStime()+"至"+ssmb.getEtime());
					// 实施内容
					builder.moveToCell(tableIndex, j, 1, 0);
					builder.write(ssmb.getContent());
					j++;
					if ((j - SSMBROWINDEX) >= SSMBROWMAX) {
						break;
					}
				}
			}
			// 家庭主要成员及社会关系
			if (peoples != null && peoples.size() > 0) {
				builder.moveToBookmark("peoples_table");

				int j = PEOPLEROWINDEX;// 开始表格Row, 这里的index从0开始
				int tableIndex = TABLEINDEXPEOPLE;

				for (ItemPeople people : peoples) {
					//姓名
					builder.moveToCell(tableIndex, j, 0, 0);
					builder.write(people.getChinName());
					//性别
					builder.moveToCell(tableIndex, j, 1, 0);
					builder.write(BaseCodeUtils.getInfoName("002", principal.getSex()));
					//出生年月
					builder.moveToCell(tableIndex, j, 2, 0);
					builder.write(people.getBorn());
					//学位
					builder.moveToCell(tableIndex, j, 3, 0);
					builder.write(BaseCodeUtils.getInfoName("006", principal.getHighestOffer()));
					//职称
					builder.moveToCell(tableIndex, j, 4, 0);
					builder.write(BaseCodeUtils.getInfoName("014", principal.getTechTitle()));
					//专业
					builder.moveToCell(tableIndex, j, 5, 0);
					builder.write(people.getSpecialty());
					//承担任务
					builder.moveToCell(tableIndex, j, 6, 0);
					builder.write(people.getTask());
					//单位
					builder.moveToCell(tableIndex, j, 7, 0);
					builder.write(people.getUnit());
					
					j++;
					if ((j - PEOPLEROWINDEX) >= PEOPLEROWMAX) {
						break;
					}
				}
			}
			
			//文档合并
			if(annex002List!=null&&annex002List.size()>0)
			{
				for(ItemAnnex itemAnnex : annex002List)
				{
					doc.appendDocument(new Document(itemAnnex.getFilePath()+itemAnnex.getAttachName()), ImportFormatMode.KEEP_SOURCE_FORMATTING);
				}

			}
			
			doc.save(saveDocFile);
		} catch (Exception ex) {
			ex.printStackTrace();
			try { 
				doc = new Document(templateFile);
				doc.save(saveDocFile);
			} catch (Exception e) {
				e.fillInStackTrace();
			}
		}
	}

}
